package com.wmyg.antifake.service.impl;

import com.wmyg.antifake.entity.BkUser;
import com.wmyg.antifake.entity.Permission;
import com.wmyg.antifake.entity.Role;
import com.wmyg.antifake.enums.Roles;
import com.wmyg.antifake.repository.BkUserRepository;
import com.wmyg.antifake.service.BkUserService;
import com.wmyg.antifake.utils.SmsUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 * @author 王卓逸
 * @program TODO
 * @description TODO
 * @create 2019-05-18
 */
@Slf4j
@Service
public class BkUserServiceImpl implements BkUserService {

    @Autowired
    BkUserRepository bkUserRepository;

    @Autowired
    PasswordEncoder passwordEncoder;

    @Override
    public BkUser findByPhone(String phone) {
        return bkUserRepository.findByPhone(phone);
    }

    @Override
    public Boolean signUp(BkUser user) {
        BkUser old = bkUserRepository.findByUsername(user.getUsername());
        if (null != old) {
            return false;
        }
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        Role role = new Role();
        role.setRoleId(Roles.USER.value());
        user.getRoles().add(role);
        bkUserRepository.save(user);
        return true;
    }

    @Override
    public BkUser findByUsername(String username) {
        BkUser user = bkUserRepository.findByUsername(username);
        user.setPassword(null);
        List<Role> roles = user.getRoles();
        for (Role role :
                roles) {
            List<Permission> permissions = role.getPermissions();
            permissions.removeIf(permission -> permission.getType() != 0 || permission.getStatus() != 0);
        }
        return user;
    }

    @Override
    public Boolean changePassword(String password, String username) {
        BkUser user = bkUserRepository.findByUsername(username);
        user.setPassword(passwordEncoder.encode(password));
        return true;
    }

    @Override
    public Boolean changePassword(String password, String username, String phone) {
        BkUser user = bkUserRepository.findByUsername(username);
        if (!phone.equals(user.getPhone())) {
            return false;
        }
        user.setPassword(passwordEncoder.encode(password));
        bkUserRepository.save(user);
        return true;
    }

    @Override
    public Page<BkUser> getAuditUserPage(Integer index) {
        return bkUserRepository.findByAuditStateOrderByRegTimeDesc(1, PageRequest.of(index, 10));
    }

    @Override
    public Page<BkUser> getPassUserPage(Integer index) {
        return bkUserRepository.findByAuditStateOrderByRegTimeDesc(2, PageRequest.of(index, 10));
    }

    @Override
    public Boolean allowAudit(Integer userId,Integer pass){
        BkUser user = bkUserRepository.findByUserId(userId);
        if(pass==1) {
            Boolean ok = SmsUtils.sendAuditSms(user.getPhone());
            if (ok == false) {
                log.info("send sms faild: {}", user.getPhone());
                return false;
            } else {
                log.info("send sms successfully: {}", user.getPhone());
                user.setAuditState(2);
                bkUserRepository.save(user);
                return true;
            }
        }
        else{

            Boolean ok = SmsUtils.sendNotAuditSms(user.getPhone());
            if (ok == false) {
                log.info("send sms faild: {}", user.getPhone());
                return false;
            } else {
                log.info("send sms successfully: {}", user.getPhone());
                user.setAuditState(3);
                bkUserRepository.save(user);
                return true;
            }

        }

    }
}
